﻿//using Microsoft.Experimental.IO;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace CommonUtils
{
    public class FileFolderHelper
    {
        public static void Move(string oldPath, string newPath)
        {
            try
            {
                File.Move(oldPath, newPath);
            }
            catch(Exception ex)
            {
                Exception ept = ex;
            }
        }

        public static void Copy(string oldPath, string newPath, bool isOverWrite = true)
        {
            try
            {
                File.Copy(oldPath, newPath, isOverWrite);
            }
            catch (Exception ex)
            {
                Exception ept = ex;
            }
        }


        public static bool LongPathDirExist(string path)
        {
            try
            {
                return Directory.Exists(path);
            }
            catch (Exception ex)
            {
                Exception ept = ex;
                return false;
            }
        }

        public static Exception LongPathDirCreate(string path)
        {
            try
            {
                Directory.CreateDirectory(path);
            }
            catch (Exception ex)
            {
                Exception ept = ex;
                return ex;
            }

            return null;
        }

        public static Exception CreateFolder(string path)
        {
            try
            {
                if (Directory.Exists(path))
                    return null;

                int startPos = 0;

                while (true)
                {
                    int pos = path.IndexOf('\\', startPos);
                    if (pos > 0)
                    {
                        startPos = pos + 1;
                        string parentPath = path.Substring(0, pos);
                        if (!Directory.Exists(parentPath))
                        {
                            try
                            {
                                Directory.CreateDirectory(parentPath);
                            }
                            catch(Exception ex)
                            {
                                return ex;
                            }
                        }
                        
                    }
                    else
                    {
                        if (!Directory.Exists(path))
                            Directory.CreateDirectory(path);

                        break;
                    }
                }
            }
            catch(Exception ex)
            {
                return ex;
            }
            return null;
        }

        public static string GetPathRoot(string fullPath)
        {
            try
            {
                //    string path = Path.GetPathRoot(fullPath);
                string path = fullPath;

                if (string.IsNullOrEmpty(fullPath))
                    return string.Empty;

                string rr = string.Empty;

                if (fullPath.Length > 3)
                {
                    path = fullPath.Substring(0, 3);
                }

                return path;
            }
            catch(Exception ex)
            {
                Exception ept = ex;
                return "";
            }
        }

        public static string GetParentPath(string fullPath)
        {
            try
            {
                if (string.IsNullOrEmpty(fullPath))
                    return string.Empty;

                int pos = fullPath.LastIndexOf('\\');
                if (pos > 0)
                {
                    return fullPath.Substring(0, pos);
                }
            }
            catch(Exception ex)
            {
                Exception ept = ex;
            }

            return string.Empty;
        }

        public static void CopyFolder(string sourceFolder, string toFolder)
        {
            try
            {
                if (!Directory.Exists(sourceFolder))
                {
                    return;
                }
                
                FileFolderHelper.CreateFolder(toFolder);
                if (Directory.Exists(toFolder))
                {
                    DirectoryInfo diSource = new DirectoryInfo(sourceFolder);
                    DirectoryInfo diTo = new DirectoryInfo(toFolder);
                    diTo.Attributes = diSource.Attributes;
                }
                foreach (string file in Directory.GetFileSystemEntries(sourceFolder))
                {
                    FileInfo info = new FileInfo(file);
                    string toPath = Path.Combine(toFolder, info.Name);

                    if (File.Exists(file))
                    {
                        File.Copy(file, toPath, true);
                    }
                    else
                    {
                        CopyFolder(file, toPath);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("*******************copy error:");
                Debug.WriteLine(ex);
            }

        }

        public static void DeleteFile(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
                return;
            if (!File.Exists(fileName))
                return;

            try
            {
                File.SetAttributes(fileName, System.IO.FileAttributes.Normal);
                File.Delete(fileName);
            }
            catch (Exception e)
            {
                Exception ept = e;
            }
        }

        public static void DeleteFolder(string dir)
        {
            try
            {
                if (string.IsNullOrEmpty(dir))
                    return;

                if (Directory.Exists(dir))
                {
                    if (dir.Length <= 3)
                        return;

                    foreach (string d in Directory.GetFileSystemEntries(dir))
                    {
                        if (File.Exists(d))
                        {
                            FileInfo fi = new FileInfo(d);
                            if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                                fi.Attributes = System.IO.FileAttributes.Normal;
                            File.Delete(d);
                        }
                        else
                            DeleteFolder(d);
                    }
                    Directory.Delete(dir);
                }
            }
            catch(Exception ex)
            {
                Debug.WriteLine($"Fail to delete:{dir} Exception:{ex}");
            }
        }

        public static bool IsFileExist(string fileName)
        {
            return File.Exists(fileName);
        }

        public static int GetPathLevel(string path)
        {
            string trimPath = path.TrimEnd('\\');
            if (string.IsNullOrEmpty(trimPath))
                return 0;

            string[] lev = trimPath.Split('\\');
            if(lev != null)
            {
                return lev.Length;
            }

            return 0;
        }

        public static bool IsFolderExist(string folderName)
        {
            return LongPathDirExist(folderName);
        }

        public static string GetFolderName(string path)
        {
            return Path.GetDirectoryName(path);
        }

        public static void RenameFolder(string oldName, string newName)
        {
            if (string.IsNullOrEmpty(oldName) || string.IsNullOrEmpty(newName))
                return;
            if (Directory.Exists(oldName))
            {
                try
                {
                    if (oldName.EndsWith("\\"))
                        oldName = oldName.Substring(0, oldName.Length - 1);
                    string tempFolderName = $"{oldName}_temp_{Guid.NewGuid().ToString()}";
                    Directory.Move(oldName, tempFolderName);
                    if (newName.EndsWith("\\"))
                        newName = newName.Substring(0, newName.Length - 1);
                    Directory.Move(tempFolderName, newName);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Fail to rename with Exception:{ex}");
                }
                
            }
        }

        public static string GetCurrentPath()
        {
            string processFullName = Process.GetCurrentProcess().MainModule.FileName;
            string folder = GetParentPath(processFullName);
            return folder;
        }
    }
}
